Inheritance (object-oriented programming)
part 12/28 · 45.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Visibility of inherited members
The following table shows which variables and functions get inherited dependent on the visibility given when deriving the class, using the terminology established by C++.cite-ref-15[14]
| Base class visibility | Derived class visibility | Derived class visibility | Derived class visibility |
|---|---|---|---|
| | Private derivation | Protected derivation | Public derivation |
| Private → Protected → Public → | Not inherited Private Private | Not inherited Protected Protected | Not inherited Protected Public |
Applications
Inheritance is used to co-relate two or more classes to each other.
Overriding
Many object-oriented programming languages permit a class or object to replace the implementation of an aspect—typically a behavior—that it has inherited. This process is called overriding. Overriding introduces a complication: which version of the behavior does an instance of the inherited class use—the one that is part of its own class, or the one from the parent (base) class? The answer varies between programming languages, and some languages provide the ability to indicate that a particular behavior is not to be overridden and should behave as defined by the base class. For instance, in C#, the base method or property can only be overridden in a subclass if it is marked with the virtual, abstract, or override modifier, while in programming languages such as Java, different methods can be called to override other methods.cite-ref-16[15] An alternative to overriding is hiding the inherited code.
Code reuse